package com.example.pinkcat.activity.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;

import com.example.pinkcat.R;
import com.example.pinkcat.activity.api.entity.VideoEntity;
import com.example.pinkcat.activity.api.getData.GetData;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class U_viewpager_adapter extends PagerAdapter {
    private Context context;
    private String[] titles;
    private ArrayList<View> views;  //每个选项卡对应的布局
    String user_id="-1";



    public U_viewpager_adapter(String[] titles, ArrayList<View> views, Context context,String user_id){
        this.titles=titles;
        this.views=views;
        this.context=context;
        this.user_id=user_id;
    }

    //实例化选项卡
    @NotNull
    @SuppressLint("ShowToast")
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View v = views.get(position);
        container.addView(v);
        //两个页面布局一样
        RecyclerView recyclerView=v.findViewById(R.id.user_viewpager_page2_recycleview);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        U_recyleview_adapter adapter1=new U_recyleview_adapter(context,10,false);//收藏
        U_recyleview_adapter adapter2=new U_recyleview_adapter(context,10,false);//动态
        if (position==0){//每个页面使用不同的数据，建立两个adpter
            recyclerView.setAdapter(adapter1);
            GetData.getMyVideo(context,adapter1,false,user_id);
        }else {
            recyclerView.setAdapter(adapter2);
            GetData.getMyVideo(context,adapter2,true,user_id);
        }


        //排序方式
        TextView sort_way_viewbnt=v.findViewById(R.id.text_sort_way);//按钮
        TextView textView=v.findViewById(R.id.text_pl);//排序说明
        sort_way_viewbnt.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("ShowToast")
            @Override
            public void onClick(View v) {
                if (sort_way_viewbnt.getText().equals("≡按播放")){
                    textView.setText(R.string.new_vd);
                    sort_way_viewbnt.setText(R.string.sort_by_time);
                    if (position==0){
                        List<VideoEntity> videoEntities= adapter1.getData();
                        Collections.sort(videoEntities, new Comparator<VideoEntity>() {
                            public int compare(VideoEntity o1, VideoEntity o2) {
                                return o2.getVideo_pub_time().compareTo(o1.getVideo_pub_time());
                            }
                        });
                        adapter1.setData(videoEntities);
                    }
                    else {
                        List<VideoEntity> videoEntities= adapter2.getData();
                        Collections.sort(videoEntities, new Comparator<VideoEntity>() {
                            public int compare(VideoEntity o1, VideoEntity o2) {

                                return o2.getVideo_pub_time().compareTo(o1.getVideo_pub_time());
                            }
                        });
                        adapter2.setData(videoEntities);
                    }
                }
                else if(sort_way_viewbnt.getText().equals("≡按时间")) {
                    textView.setText(R.string.like_vd);
                    sort_way_viewbnt.setText(R.string.sort_by_play);
                    if (position==0){
                        System.out.println("sadsajdia");
                        List<VideoEntity> videoEntities= adapter1.getData();
                        Collections.sort(videoEntities, new Comparator<VideoEntity>() {
                            public int compare(VideoEntity o1, VideoEntity o2) {
                                if (o2.getTotalplay()>o1.getTotalplay())
                                    return 1 ;
                                else
                                    return -1;
                            }
                        });
                        adapter1.setData(videoEntities);
                    }
                    else {
                        List<VideoEntity> videoEntities= adapter2.getData();
                        Collections.sort(videoEntities, new Comparator<VideoEntity>() {
                            public int compare(VideoEntity o1, VideoEntity o2) {
                                if (o2.getTotalplay()>o1.getTotalplay())
                                    return 1 ;
                                else
                                    return -1;
                            }
                        });
                        adapter2.setData(videoEntities);
                    }
                }
            }
        });
        return v;
    }

    //删除选项卡
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(views.get(position));
    }
    //返回选项大小
    @Override
    public int getCount() {
        return 2;
    }

    //判断是否是为返回选项卡
    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view==object;
    }

    //获取标题
    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }
}
